'''
Generates a JobDef from an XML Settings file.
'''
import os
from xml.dom.minidom import parse
#======================================================================
# EXAMPLE XML FILE
#======================================================================
"""
<!-- Settings file for Dexen generator -->
<dexenJobdef type="houdini_ea">


            
</dexenJobdef>
"""

#======================================================================
# XML GENERATOR
#======================================================================
class GeneratorSelector(object):
    def __init__(self, settings_file_path):
        self.settings_file_path = settings_file_path

    def _get_type(self, dom):
        dexenJobdef = dom.getElementsByTagName("dexenJobdef")
        #Check that the right elements exist  
        if len(dexenJobdef) != 1:
            raise Exception("ERROR in settings file. There must be exactly one 'dexenJobdef' element.")
        dexen_jobdef_elem = dexenJobdef[0]
        #Check the the elements have the right attributes
        if not dexen_jobdef_elem.hasAttribute("type"):
            raise Exception("ERROR in settings file. The dexenJobdef element must have an 'type' attribute.")
        #get the type attribute
        dexen_jobdef_type = dexen_jobdef_elem.getAttribute("type")
        return dexen_jobdef_type

    def generate(self):
        #get the path to the settings file
        jobdef_dir_path = os.path.dirname(self.settings_file_path)
        #read xml settings
        dom = parse(self.settings_file_path)
        #get the type
        jobdef_type = self._get_type(dom)
        # generate the settings
        if jobdef_type == "houdini_ea":
            from houdini_ea.generator import Generator
            gen = Generator(self.settings_file_path)
            gen.generate()   

        print "done"


#FOR TESTING
def main():
    gen = SettingsGenerator("settings.xml")
    gen.generate()
    

if __name__ == "__main__":
    main()


        